עבודה עם STL. )Dmitry Korolev

Size: px
Start display at page:

Download "עבודה עם STL. )Dmitry Korolev"

Transcription

1 עבודה עם STL )Dmitry Korolev יניב סבו )מבוסס על המאמר של

2 הקדמה STL = Standard Template Library הספרייה הסטנדטית של ++C. כוללת את רוב האלגוריתמים ומבני הנתונים הבסיסיים במדעי המחשב. Heavily parameterized כמעט כל רכיב ב STL הוא.template

3 Containers STL כולל :containers classes מחלקות שתפקידן להכיל אובייקטים אחרים. לדוגמה, STL כולל את המחלקות: deque, vector, list, set, multiset, map, multimap,

4 לפני שנתחיל כשתוכנית משתמשת ב,STL היא צריכה לכלול את ה headers המתאימית. #include <stack> כל ה- STL מרוכז ב.namespace std using namespace std; כשיוצרים container יש לספק גם את סוג הנתונים שהוא יכיל. vector<int> N; vector< vector<int> > CorrectDefinition; vector<vector<int>> WrongDefinition; // Wrong: compiler may be confused by 'operator >>'

5 Vector vector<int> v(10); for(int i = 0; i < 10; i++) { v[i] = (i+1)*(i+1); vector<int> v; vector<int> v[10]; וקטור הוא למעשה מערך עם פונקציות נוספות. :C לקוד backward-compatible מה שתי השורות הבאות יעשו? וקטור יכול לדווח על הגודל שלו: int elements_count = v.size(); יש לשים לב כי size() הוא!unsigned

6 - המשך Vector איך בודקים האם וקטור ריק? bool is_nonempty_notgood = (v.size() >= 0); // Try to avoid this bool is_nonempty_ok =!v.empty(); vector<int> v; for(int i = 1; i < ; i *= 2) { v.push_back(i); :vector vector<int> v(20); for(int i = 0; i < 20; i++) { v[i] = i+1; v.resize(25); for(int i = 20; i < 25; i++) { v.push_back(i*2); // Writes to elements with indices [25..30), not [20..25)! < הכנסת איבר חדש ל פונקציית :resize

7 - המשך Vector שימוש בהרבה push_back עלול לגרום להקצאות זכרון מיותרות. לדוגמה, עבור וקטור שבו 1000 איברים והגודל המוקצה שלו הוא 1024, אם נוסיף 50 איברים באמצעות push_back נקבל וקטור שהגודל המוקצה שלו הוא.2048 במקום, ניתן להשתמש ב.vector.reserve(1050)

8 - המשך Vector פונקציית vector.clear() אתחול וקטור: יצירת מערך דו מימדי: - מרוקנת את הוקטור. vector<int> v1; //... vector<int> v2 = v1; vector<int> v3(v1); vector<int> Data(1000); // 1000 zeros after creation vector<string> names(20, Unknown ); vector< vector<int> > Matrix; int N, M; vector< vector<int> > Matrix(N, vector<int>(m, -1)); אתחול מטריצה:

9 - המשך Vector העברת וקטור כפרמטר לפונקציה: void some_function(vector<int> v) { // Never do it unless you re sure what you do! //... void some_function(const vector<int>& v) { // OK //... int modify_vector(vector<int>& v) { // Correct V[0]++;

10 Pairs הוא טיפוס המכיל אובייקט אחד מסוג T1 pair<t1, <T2 ואובייקט שני מסוג T2. לדוגמה: pair<string, pair<int,int> > P; string s = P.first; // extract string int x = P.second.first; // extract first int int y = P.second.second; // extract second int היתרון הגדול של pairs הוא שיש להם פונקציית השוואה built-in שמבצעת השוואה קודם לפי המפתח הראשון ואם הם שווים מבצעת השוואה לפי המפתח השני.

11 Iterators איטרטורים הם המנגנון המאפשר לגשת למידע הנמצא ב containers ומהווים בעצם הכללה של מצביעים. ממומשים בצורה גנרית. איטרטורים רגילים iterators) (normal מאפשרים: לקבל את הערך שמוצבע ע"י האיטרטור: int x = *it להגדיל ולהקטין איטרטורים: it-- it++, להשוות איטרטורים: ==,=! איטרטורים מסוג בנוסף: random access iterators מאפשרים הוספה/החסרה לאיטרטור: it+=20 שקול לביצוע איברים קדימה..int n = it1-it2 קבלת המרחק בין איטרטורים: 20 shift

12 - המשך Iterators :container לדוגמה הפיכת template<typename T> void reverse_array(t *first, T *last) { if(first!= last) { while(true) { swap(*first, *last); first++; if(first == last) { break; last--; if(first == last) { break; end האלגוריתמים ב STL משתמשים בשני :iterators begin, end כאשר begin מצביע לאיבר הראשון ו מצביע לאיבר האחד אחרי אחרון. לכל container יש שתי פונקציות: end() begin(), שמחזירות את האיטרטורים המתאימים.

13 - המשך Iterators.c.begin() == c.end() c לכן, ניתן לומר כי ריק אמ"מ עבור containers שהאיטרטורים שלהם הם random.c.end()-c.begin()=c.size() מתקיים: access iterators STL היא: לכן הפונקציה להפיכת container עבור template<typename T> void reverse_array_stl_compliant(t *begin, T *end) { // We should at first decrement 'end' // But only for non-empty range if(begin!= end) { end--; if(begin!= end) { while(true) { swap(*begin, *end); begin++; If(begin == end) { break; end--; if(begin == end) { break;

14 שימוש ב iterators כל אובייקט עם מספיק פונקציונאליות יכול להיות מועבר לאלגוריתמים ופונקציות של STL )לדוגמה מצביע(. לדוגמה: vector<int> v; //... vector<int> v2(v); vector<int> v3(v.begin(), v.end()); // v3 equals to v2 int data[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 ; vector<int> primes(data, data+(sizeof(data) / sizeof(data[0]))); // not recommended vector<int> v; //... vector<int> v2(v.begin(), v.begin() + (v.size()/2)); int data[10] = { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 ; reverse(data+2, data+6); // the range { 5, 7, 9, 11 is now { 11, 9, 7, 5 ;

15 - שימוש ב iterators המשך לכל container יש גם את הפונקציות rend() rbegin(), שמחזירות.reverse iterators vector<int> v; vector<int> v2(v.rbegin()+(v.size()/2), v.rend()); כדי ליצור איטרטור עלינו לציין את הטיפוס שעליו הוא מצביע. ניתן לייצר איטרטור עבור container באמצעות הוספת ::reverse_iterator, ::iterator, ::const_iterator,.container לשם ה ::const_reverse_iterator vector<int> v; //... // Traverse all container, from begin() to end() for(vector<int>::iterator it = v.begin(); it!= v.end(); it++) { *it++; // Increment the value iterator is pointing to

16 אלגוריתמים STL כולל גם אלגוריתמים שניתן להפעיל על איטרטורים. אלגוריתם reverse שראינו. אלגוריתם - find מקבל שני איטרטורים וערך לחיפוש. מחזיר את האיטרטור שמצביע להופעה הראשונה של הערך אם נמצא בין האיטרטורים ואחרת את האיטרטור הסופי )כלומר הימני מבין שני האיטרטורים(. כדי לקבל את אינדקס האיבר שנמצא יש לחסר את האיטרטור של ההתחלה מהתוצאה של.find vector<int> v; if(find(v.begin(), v.end(), 49)!= v.end()) { //...

17 אלגוריתמים - המשך - min_element, max_element מחזירים את האיטרטור לאיבר המתאים )מקסימלי/מינימלי(. int data[5] = { 1, 5, 2, 4, 3 ; vector<int> X(data, data+5); int v1 = *max_element(x.begin(), X.end()); // Returns value of max element in vector int i1 = min_element(x.begin(), X.end()) X.begin; // Returns index of min element in vector int v2 = *max_element(data, data+5); // Returns value of max element in array int i3 = min_element(data, data+5) data; // Returns index of min element in array - ממיין את האיברים שבין שני האיטרטורים: vector<int> X; Int A[10]; //... sort(x.begin(), X.end()); // Sort array in ascending order sort(a, A+10); // Sort A in ascending order sort(x.rbegin(), X.rend()); // Sort array in descending order using with reverse iterators sort

18 אלגוריתמים - המשך מה נעשה אם נרצה למיין טיפוסים לא פרמיטיביים? sort() מבוסס על אותה טכניקה שמופיעה בכל המקומות הרלוונטיים ב- STL : השוואות מתבצעות באמצעות אופרטור >. struct fraction { int n, d; // (n/d) bool operator < (const fraction& f) const { return n*f.d < f.n*d; ; vector<fraction> v; sort(all(v));

19 אלגוריתמים - המשך מה נעשה אם נרצה למיין במספר צורות את אותו טיפוס? typedef pair<double, double> dd; const double epsilon = 1e-6; struct sort_by_polar_angle { dd center; template<typename T> sort_by_polar_angle(t b, T e) { //. bool operator () (const dd& a, const dd& b) const { double p1 = atan2(a.second-center.second, a.first-center.first); double p2 = atan2(b.second-center.second, b.first-center.first); return p1 + epsilon < p2; ; vector<dd> points; sort(points.begin(), points.end(), sort_by_polar_angle(points.begin(), points.end()));

20 אלגוריתמים - המשך אלגוריתמים נוספים: b).max(a, b), min(a, b), swap(a, אלגוריתמים שימושיים נוספים הם next_permutation, next_permutation(begin, end).prev_permutation מכניס לתחום end) (begin, את הפרומטציה הבאה של אותם איברים, או מחזירה false אם זו האחרונה. vector<int> v; for(int i = 0; i < 10; i++) { v.push_back(i); do { Solve(..., v); while(next_permutation(v.begin(), v.end());

21 אלגוריתמים - המשך אלגוריתם to_begin) copy(from_begin, from_end, מעתיק איברים מהתחום הראשון לשני. בתחום השני צריך להיות מספיק מקום. vector<int> v1; vector<int> v2; //... // Now copy v2 to the end of v1 v1.resize(v1.size() + v2.size()); // Ensure v1 have enough space copy(v2.begin(), v2.end(), v1.end() - v2.size()); // Copy v2 elements right after v1 ones

22 הערות void f(const vector<int>& v) { הקוד הבא ייצור שגיאה: for( vector<int>::iterator it = v.begin(); // hm... where s the error?.. //... הקוד הנכון נראה כך: void f(const vector<int>& v) { int r = 0; for(vector<int>::const_iterator it = v.begin(); it!= v.end(); it++) //

23 הערות - המשך #define sz(a) int((a).size()) #defile all(c) (c).begin(),(c).end() #define tr(c,i) for(typeof((c).begin()) i = (c).begin(); i!= (c).end(); i++) #define present(c,x) ((c).find(x)!= (c).end()) #define cpresent(c,x) (find(all(c),x)!= (c).end()) void f(const vector<int>& v) { int r = 0; tr(v, it) { r += (*it)*(*it); return r; מאקרויים שימושיים )?(: המאקרו tr מאפשר לבצע איטרציה על איברי כל container בקלות. לדוגמה:

24 שינוי המידע ב Vector vector<int> v; //... :insert() v.insert(1, 42); // Insert value 42 after the first iterator ניתן להכניס איבר לוקטור באמצעות יש לשים לב כי insert של vector גורם להזזת איברים ולכן מומלץ להכניס מספר איברים בפקודה אחת: vector<int> v; vector<int> v2; //.. // Shift all elements from second to last to the appropriate number of elements. // Then copy the contents of v2 into v. v.insert(1, all(v2)); erase(iterator); erase(begin iterator, end iterator); לוקטור יש גם פונקציית :erase

25 String.strings יש ל STL container מיוחד לעבודה עם string של STL מאפשר מספר פעולות חשובות כמו שרשור,)s1+s2( קלט של מחרוזת )s )cin << ופעולות רבות נוספות. string s = "hello"; string s1 = s.substr(0, 3), // "hel" s2 = s.substr(1, 3), // "ell" s3 = s.substr(0, s.length()-1), // "hell" s4 = s.substr(1); // "ello" מה יקרה כאשר = 0 s.length()?

26 Set עץ אדום שחור מאפשר הוספה, מחיקה וחיפוש של איברים ב.O(logN) העץ לא יכול להכיל איברים כפולים. מספר האיברים ב set מוחזר בסיבוכיות (1)O. set<int> s; for(int i = 1; i <= 100; i++) { s.insert(i); // Insert 100 elements, [1..100] s.insert(42); // does nothing, 42 already exists in set for(int i = 2; i <= 100; i += 2) { s.erase(i); // Erase even values int n = int(s.size()); // n will be 50.set כמובן שלא ניתן לבצע push_back() על

27 - המשך Set מעבר על האיברים בסדר עולה: // Calculate the sum of elements in set set<int> S; //... int r = 0; for(set<int>::const_iterator it = S.begin(); it!= S.end(); it++) { r += *it; ניתן גם להשתמש במאקרו שהגדרנו: set< pair<string, pair< int, vector<int> > > SS; int total = 0; tr(ss, it) { total += it->second.first;

28 - המשך Set אסור להשתמש באלגוריתם find של STL כדי לחפש איבר ב - set זה יתבצע ב.O(n) במקום זאת יש לקרוא ל.set::find set::find מחזיר איטרטור לאיבר שנמצא, או end() אם לא נמצא. set<int> s; //... if(s.find(42)!= s.end()) { // 42 presents in set else { // 42 not presents in set

29 - המשך Set.erase כדי למחוק איבר מה set משתמשים ב set<int> s; // s.insert(54); s.erase(29); set<int> s; // s.insert(54); s.erase(29); set יכול גם לקבל מערך ב.constructor ניתן להשתמש בזה כדי להסיר כפילויות ולמיין במהירות: vector<int> v; // set<int> s(all(v)); vector<int> v2(all(s));

30 Map דומה מאוד ל,set אך הוא מכיל זוגות value>.pair<key, יכול להיות לכל היותר pair אחד עם אותו.key ב map<string, int> M; M["Top"] = 1; M["Coder"] = 2; M["SRM"] = 10; int x = M["Top"] + M["Coder"]; if(m.find("srm")!= M.end()) { M.erase(M.find("SRM")); // or even M.erase("SRM") map<string, int> M; // int r = 0; tr(m, it) { r += it->second; :keys.map map map map האופרטור [] מוגדר על ניתן לבצע מעבר על לפי סדר ה

31 - המשך Map יש הבדל משמעותי בין find() לאופרטור [] - find לא משנה את התוכן של,map בעוד שהאופרטור [] יוצר איבר אם הוא לא נמצא ב.map void f(const map<string, int>& M) { if(m["the meaning"] == 42) { // Error! Cannot use [] on const map objects! if(m.find("the meaning")!= M.end() && M.find("the meaning")->second == 42) { // Correct cout << "Don't Panic!" << endl; כשעובדים עם set ו- map, אין לשנות את הערך של האיברים באמצעות האיטרטורים!

32 שימוש באובייקטים שונים ב map/set שוב, לפי אותו כלל: השוואות מבוצעות באמצעות אופרטור >. const double epsilon = 1e-7; struct point { double x, y; // Declare operator < taking precision into account bool operator < (const point& p) const { if(x < p.x - epsilon) return true; if(x > p.x + epsilon) return false; if(y < p.y - epsilon) return true; if(y > p.y + epsilon) return false; return false; ; כעת ניתן ליצור set<point> או.map<point,string>

33 hash_set / hash_map hash_set, hash_map יעילים כאשר אנו מעוניינים לבדוק האם איבר נמצא ב container )למימוש,)dictionaries אך אין בהם חשיבות לסדר. char*, hash ב- STL.int ישנן פונקציות לחלק מהטיפוסים:

34 - המשך hash_set / hash_map hash_set<int> a_hash_set(size); hash_map<int, int> a_hash_map(size); hash_set<element, hash_elem, eq_elem> elem_hash_set(size); hash_map<element, int, hash_elem, eq_elem> elem_hash_map(size); struct element { int kuku; int kaka; char str[char_arr_size]; ; struct hash_elem { hash<int> h; size_t operator()(const element& e) const { return (h.operator ()(e.kaka)<<16)+h.operator ()(e.kuku); ; struct eq_elem { bool operator()(const element& e1, const element& e2) const { for(int i = 0; i < CHAR_ARR_SIZE; i++) if (e1.str[i]!= e2.str[i]) return false; return e1.kuku == e2.kuku && (e1.kuku == e2.kuku); ;

35 String Streams istringstream, void f(const string& s) { // Construct an object to parse strings istringstream is(s); לעיתים יש צורך לעבד.strings ++C מספקת שני אובייקטים לכך:.ostringstream // Vector to store data vector<int> v; // Read integer while possible and add it to the vector int tmp; while(is >> tmp) { v.push_back(tmp);

36 - המשך String Streams string f(const vector<int>& v) { // Construct an object to do formatted output ostringstream os; // Copy all elements from vector<int> to string stream as text tr(v, it) { os << ' ' << *it; // Get string from string stream string s = os.str(); // Remove first space character if(!s.empty()) { // Beware of empty string here s = s.substr(1); return s;

37 DFS ברי הגעה מ- s. שהם אלגוריתם לגילוי כל הצמתים ב- V בנוסף מאפשר למצוא האם יש מעגלים בעץ, טופולוגי, למצוא רכיבים קשירים היטב. לבצע מיון הרעיון: בכל שלב האלגוריתם מנסה להתקדם לעומק - כאשר נבקר בצומת v, אם יש קשת (v,u) לצומת u שעוד לא נתגלתה נבקר בה ונמשיך את החיפוש ממנה.

38 DFS using STL typedef vector<int> vi; typedef vector<vi> vvi; int N; // number of vertices vvi W; // graph vi V; // V is a visited flag void dfs(int i) { if(!v[i]) { V[i] = true; for_each(all(w[i]), dfs); bool check_graph_connected_dfs() { int start_vertex = 0; V = vi(n, false); dfs(start_vertex); return (find(all(v), 0) == V.end());

39 BFS ברי הגעה מ- s. שהם אלגוריתם לגילוי כל הצמתים ב- V בנוסף מאפשר למצוא את המרחק הקצר ביותר )בקשתות( מ- s לכל צומת ב- V. הרעיון: בכל איטרציה האלגוריתם בוחן חזית של צמתים במרחק i מ- s. יש לגלות את כל הצמתים במרחק i מ- s לפני גילוי צומת במרחק 1+i.

40 BFS using STL int N; // number of vertices vvi W; // lists of adjacent vertices bool check_graph_connected_bfs() { int start_vertex = 0; vi V(N, false); queue<int> Q; Q.push(start_vertex); V[start_vertex] = true; while(!q.empty()) { int i = Q.front(); // get the tail element from queue Q.pop(); tr(w[i], it) { if(!v[*it]) { V[*it] = true; Q.push(*it); return (find(all(v), 0) == V.end());

41 Dijkstra.s קלט: גרף ממושקל מכוון ללא קשתות שליליות וצומת.d[v] = dist(s,v) פלט: v לכל צומת

42 Dijkstra pseudo code /* Initialization: set every distance to INFINITY until we discover a path */ for i = 0 to V - 1 end dist[i] = INFINITY prev[i] = NULL /* The distance from the source to the source is defined to be zero */ dist[s] = 0 /* This loop corresponds to sending out the explorers walking the paths, where * the step of picking "the vertex, v, with the shortest path to s" corresponds * to an explorer arriving at an unexplored vertex */ for each edge of v, (v1, v2) /* The next step is sometimes given the confusing name "relaxation" if(dist[v1] + length(v1, v2) < dist[v2]) dist[v2] = dist[v1] + length(v1, v2) prev[v2] = v1 possibly update U, depending on implementation end if end for end while while(f is missing a vertex) pick the vertex, v, in U with the shortest path to s add v to F

43 - המשך Dijkstra קל מאוד לממש את האלגוריתם של dijkstra מערך מרחקים בסיבוכיות ) 2.O(V בעזרת עבור גרפים דלילים ניתן לממש את האלגוריתם בסיבוכיות O(ElogV) באמצעות ערימה שתומכת ב decrease key )בעזרת ערימות פיבונצ'י ניתן אף לממש בסיבוכיות.)O(E+VlogV) לצערנו, בערימה של )priority_queue( STL אין פונקציית.decrease key מה נעשה?

44 Dijkstra using STL vi D(N, ); // distance from start vertex to each vertex priority_queue<ii,vector<ii>, greater<ii> > Q; // priority_queue with reverse comparison operator, // so top() will return the least distance // initialize the start vertex, suppose it s zero D[0] = 0; Q.push(ii(0,0)); // iterate while queue is not empty while(!q.empty()) { // fetch the nearest element ii top = Q.top(); Q.pop(); // v is vertex index, d is the distance int v = top.second, d = top.first; // this check is very important // we analyze each vertex only once // the other occurrences of it on queue (added earlier) // will have greater distance if(d <= D[v]) { // iterate through all outcoming edges from v tr(g[v], it) { int v2 = it->first, cost = it->second; if(d[v2] > D[v] + cost) { // update distance if possible D[v2] = D[v] + cost; // add the vertex to queue Q.push(ii(D[v2], v2));

לתיכנות עם MATLAB Lecture 5: Boolean logic and Boolean expressions

לתיכנות עם MATLAB Lecture 5: Boolean logic and Boolean expressions מבוא לתיכנות עם MATLAB 234127 Lecture 5: Boolean logic and Boolean expressions Written by Prof. Reuven Bar-Yehuda, Technion 2013 Based on slides of Dr. Eran Eden, Weizmann 2008 1 >>g = [89 91 80 98]; >>p

More information

לתיכנות עם MATLAB Lecture 5: Boolean logic and Boolean expressions

לתיכנות עם MATLAB Lecture 5: Boolean logic and Boolean expressions מבוא לתיכנות עם MATLAB 23427 Lecture 5: Boolean logic and Boolean expressions Written by Prof. Reuven Bar-Yehuda, Technion 203 Based on slides of Dr. Eran Eden, Weizmann 2008 ביטויים לוגיים דוגמא: תקינות

More information

Practical Session No. 14 Topological sort,amortized Analysis

Practical Session No. 14 Topological sort,amortized Analysis Practical Session No. 14 Topological sort,amortized Analysis Topological- Sort Topological sort Ordering of vertices in a directed acyclic graph (DAG) G=(V,E) such that if there is a path from v to u in

More information

Algorithms. Intro2CS week 5

Algorithms. Intro2CS week 5 Algorithms Intro2CS week 5 1 Computational problems A computational problem specifies an inputoutput relationship What does the input look like? What should the output be for each input? Example: Input:

More information

ספרית התבניות הסטנדרטית (STL) כתיבת אלגוריתמים גנריים מצביעים חכמים. .vector. list iterator נכיר תחילה את האוסף הפשוט ביותר בספריה

ספרית התבניות הסטנדרטית (STL) כתיבת אלגוריתמים גנריים מצביעים חכמים. .vector. list iterator נכיר תחילה את האוסף הפשוט ביותר בספריה ספרית התבניות הסטנדרטית (STL) כתיבת אלגוריתמים גנריים מצביעים חכמים vector list iterator 2 קיימת בכל מימוש של ++C מכילה אוספים (Containers) ואלגוריתמים נכיר תחילה את האוסף הפשוט ביותר בספריה.vector מערך

More information

לתיכנות עם MATLAB Lecture 5: Boolean logic and Boolean expressions

לתיכנות עם MATLAB Lecture 5: Boolean logic and Boolean expressions מבוא לתיכנות עם MATLAB 234127 Lecture 5: Boolean logic and Boolean expressions Written by Prof. Reuven Bar-Yehuda, Technion 2013 Based on slides of Dr. Eran Eden, Weizmann 2008 1 motivation Proper academic

More information

Amortized Analysis, Union-Find,

Amortized Analysis, Union-Find, Practical Session No. 13 Amortized Analysis, Union-Find, AMORTIZED ANALYSIS Refers to finding the average running time per operation, over a worst-case sequence of operations. Amortized analysis differs

More information

תרגול 12. Standard Template Library כתיבת אלגוריתמים גנריים מצביעים חכמים

תרגול 12. Standard Template Library כתיבת אלגוריתמים גנריים מצביעים חכמים תרגול 12 Standard Template Library כתיבת אלגוריתמים גנריים מצביעים חכמים ספרית התבניות הסטנדרטית קיימת בכל מימוש של ++C מכילה אוספים (Containers) ואלגוריתמים. משתמשת בתבניות :(templates) אוספי הנתונים

More information

מערכים שעור מס. 4 כל הזכויות שמורות דר ' דרור טובי המרכז האוניברסיטאי אריאל 1

מערכים שעור מס. 4 כל הזכויות שמורות דר ' דרור טובי המרכז האוניברסיטאי אריאל 1 מערכים שעור מס. 4 דרור טובי דר' כל הזכויות שמורות דר ' דרור טובי המרכז האוניברסיטאי אריאל 1 למה מערכים? ברצוננו לאחסן בתוכנית ציוני בחינה כדי לחשב את ממוצע הציונים וסטיית התקן. נניח ש 30 סטודנטים לקחו

More information

מבוא למדעי המחשב תרגול 13: עצים בינאריים

מבוא למדעי המחשב תרגול 13: עצים בינאריים מבוא למדעי המחשב תרגול 13: עצים בינאריים עצים בינאריים - הגדרה הגדרה: עץ בינארי הוא עץ ריק (בלי צמתים) או עץ המורכב משורש ושני תתי-עצים, הוא עץ בינארי. ימני ושמאלי, שכל אחד מהם תרגיל 1 עץ בינארי מסודר

More information

תזכורת: עץבינארי מבוא למדעי המחשב הרצאה 24: עצי חיפוש בינאריים

תזכורת: עץבינארי מבוא למדעי המחשב הרצאה 24: עצי חיפוש בינאריים מבוא למדעי המחשב הרצאה 2: עצי חיפוש בינאריים תזכורת: עץבינארי בנוסףלרשימהמקושרת ומערך, הצגנומבנהנתונים קונקרטיחדש עץבינארי עץבינארימורכבמ: שורש תת-עץשמאלי תת-עץימני A B C D E F G 2 תזכורת: שורש ותתי-עצים

More information

Practical Session - Heap

Practical Session - Heap Practical Session - Heap Heap Heap Maximum-Heap Minimum-Heap Heap-Array A binary heap can be considered as a complete binary tree, (the last level is full from the left to a certain point). For each node

More information

מצליחה. 1. int fork-bomb() 2. { 3. fork(); 4. fork() && fork() fork(); 5. fork(); printf("bla\n"); 8. return 0; 9. }

מצליחה. 1. int fork-bomb() 2. { 3. fork(); 4. fork() && fork() fork(); 5. fork(); printf(bla\n); 8. return 0; 9. } שאלה : (4 נקודות) א. ב. ג. (5 נקודות) הגדירו את המונח race-condition במדוייק לא להשמיט פרטים. ספקו דוגמא. (5 נקודות) מהו? Monitor נא לספק הגדרה מלאה. ( נקודות) ( נקודות) ציינו כמה תהליכים יווצרו בקוד הבא

More information

תור שימושים בעולם התוכנה

תור שימושים בעולם התוכנה מבוא למדעי המחשב הרצאה : Queue, Iterator & Iterable תור מבנה נתונים אבסטרקטי תור שימושים בעולם התוכנה השימושים של תורים בעולם התוכנה מזכירים מאוד תורים במציאות: )VoIP( )YouTube( מקלדת שידור סרט באינטרנט

More information

רזח יליגרתו םי יראני ב ם

רזח יליגרתו םי יראני ב ם מבוא למדעי המחשב עצים בינאריים ותרגילי חזרה תרגול 13: עצים בינאריים - הגדרה הגדרה: עץ בינארי הוא עץ ריק )בלי צמתים( או עץ המורכב משורש ושני תתי-עצים, הוא עץ בינארי. ימני ושמאלי, שכל אחד מהם שאלה עץ בינארי

More information

מבוא לתכנות בשפת C. Tzachi (Isaac) Rosen

מבוא לתכנות בשפת C. Tzachi (Isaac) Rosen מבוא לתכנות בשפת C מצביעים והקצאה דינאמית כתובות של משתנים לכל משתנה כתובת של המקום שלו בזיכרון כבר ראינו: שם של מערך הוא למעשה הכתובת של התא הראשון )באינדקס 0( של המערך להזכירכם: תא של מערך הינו משתנה

More information

משתנים שעור מס. 2 כל הזכויות שמורות דר ' דרור טובי המרכז האוניברסיטאי אריאל 1

משתנים שעור מס. 2 כל הזכויות שמורות דר ' דרור טובי המרכז האוניברסיטאי אריאל 1 משתנים שעור מס. 2 דרור טובי דר' כל הזכויות שמורות דר ' דרור טובי המרכז האוניברסיטאי אריאל 1 תפקיד המשתנים הצהרה על משתנה השמת ערך במשתנה int a, b, c; a = 1234; b = 99; c = a + b; משתנים מאפשרים לנו לשמור

More information

ת ונכת סרוק תורשוקמ תומישר :יעישת רועיש 1

ת ונכת סרוק תורשוקמ תומישר :יעישת רועיש 1 קורס תכנות שיעור תשיעי: רשימות מקושרות 1 הקצאה דינאמית של מערכים דו-ממדיים )לפחות( שלוש גישות אפשריות:.1 מערך של מערכים מצביעים לתוך מערך "גדול".2 3. מצביע יחיד למערך גדול 2 The Interface 3 (Simple) Usage

More information

מבוא למדעי המחשב תרגול 8 רשימה משורשרת כללית, Comparator

מבוא למדעי המחשב תרגול 8 רשימה משורשרת כללית, Comparator מבוא למדעי המחשב 2017 תרגול 8 רשימה משורשרת כללית, Comparator בתרגול היום. LinkedList בניית ההכללה מ- LinkIntList תרגול המבנה ושימושיו ממשקים: Comparator Sorted Linked List ל- LinkedList ע"י שימוש ב- Comparator

More information

סכום (סדרת ערכים) אחרת - דוגמא: סכום-ספרות (num) אם < 10 num החזר 1 או אם = 0 = num החזר 0 public static int numofdigits (int num)

סכום (סדרת ערכים) אחרת - דוגמא: סכום-ספרות (num) אם < 10 num החזר 1 או אם = 0 = num החזר 0 public static int numofdigits (int num) 1 תבנית צבירה תבניות אלגוריתמיות לפעולות רקורסיביות תבנית צבירה לסדרת ערכים: סכום (סדרת ערכים) החזר את ערך הקצה + סכום (סדרת הערכים ללא ערך הקצה) דוגמא: פעולה המחזירה את סכום הספרות שבמספר שלם לא שלילי

More information

תוכנה 1 סמסטר א' תשע"א

תוכנה 1 סמסטר א' תשעא General Tips on Programming תוכנה 1 סמסטר א' תשע"א תרגול מס' 6 מנשקים, דיאגרמות וביטים * רובי בוים ומתי שמרת Write your code modularly top-down approach Compile + test functionality on the fly Start with

More information

תוכנה 1 * לא בהכרח בסדר הזה

תוכנה 1 * לא בהכרח בסדר הזה תוכנה 1 תרגול 7: מנשקים, פולימורפיזם ועוד * לא בהכרח בסדר הזה 2 מנשקים מנשקים מנשק )interface( הוא מבנה תחבירי ב- Java המאפשר לחסוך בקוד לקוח. מנשק מכיל כותרות של מתודות המימוש שלהן. )חתימות( ללא קוד אשר

More information

תכנות מונחה עצמים משחקים תשע"ו

תכנות מונחה עצמים משחקים תשעו move semantics 1 תכנות מונחה עצמים ופיתוח משחקים תשע"ו סמנטיקת ההעברה semantics( )Move move semantics 2 מטרה האצה של התוכניות, שיפור בביצועים על ידי חסכון בבנייה והעתקה של אובייקטים זמניים move semantics

More information

רשימות דילוגים Skip Lists

רשימות דילוגים Skip Lists Lecture6 of Geiger & Itai s slide brochure www.cs.technion.ac.il/~dang/courseds רשימות דילוגים Skip Lists Skip lists: A probabilistic Alternative to Balanced Trees, William Pugh, Communications of the

More information

תוכנה 1 תרגול 2: מערכים ומבני בקרה

תוכנה 1 תרגול 2: מערכים ומבני בקרה תוכנה 1 תרגול 2: מערכים ומבני בקרה 2 Useful Eclipse Shortcuts Ctrl+1 quick fix for errors, or small refactoring suggestions Ctrl+SPACE code content assist (auto-completion) Auto completion for main create

More information

Programming for Engineers in Python

Programming for Engineers in Python Programming for Engineers in Python Lecture 9: Sorting, Searching and Time Complexity Analysis Autumn 2011-12 1 Lecture 8: Highlights Design a recursive algorithm by 1. Solving big instances using the

More information

Programming for Engineers in Python

Programming for Engineers in Python Programming for Engineers in Python Lecture 9: Sorting, Searching and Time Complexity Analysis Autumn 2011-12 1 Lecture 8: Highlights Design a recursive algorithm by 1. Solving big instances using the

More information

הנכות 1 םוכיס לוגרת 13 1

הנכות 1 םוכיס לוגרת 13 1 תוכנה 1 סיכום תרגול 13 1 קצת על מנשקים מנשק יכול להרחיב יותר ממנשק אחד שירותים במנשק הם תמיד מופשטים וציבוריים public interface MyInterface { public abstract int foo1(int i); int foo2(int i); The modifiers

More information

Engineering Programming A

Engineering Programming A Engineering Programming A תרגול 5 25.11.2012 מערכים חד-מימדיים )תזכורת( לדוגמא: מערך בשם Arr בגודל 8 שאיבריו מטיפוס int 3 7 5 6 8 1 23 16 0 1 2 3 4 5 6 7 ב - arr[0] ב יושב ערך שהוא המספר השלם 3 arr[1]

More information

ASP.Net Web API.

ASP.Net Web API. ASP.Net Web API 1 מה זה? Web API View בלבד ולא Data אותו מממש השרת והוא מחזיר לקליינט API הוא Web API הבקשה והתשובה הן בפרוטוקול Http\Https הקליינטים של Web API יכולים להיות רבים : אפשר להשתמש גם בMVC

More information

הנכות 1 תואיגש םע תודדומתהו תואלול,םי : כרעמ 2 לוגרת

הנכות 1 תואיגש םע תודדומתהו תואלול,םי : כרעמ 2 לוגרת תוכנה 1 תרגול 2: מערכים, לולאות והתמודדות עם שגיאות מערכים מערכים Array: A fixed-length data structure for storing multiple values of the same type Example from last week: An array of odd numbers: Indices

More information

Simple Web Service. namespace MyService { public class Service1 : System.Web.Services.WebService {

Simple Web Service. namespace MyService { public class Service1 : System.Web.Services.WebService { Simple Web Service WS פתיחת פרוייקט File New Project ASP.Net web service project - >http://localhost/webservice1 יצירת שירות המחשב חיבור וחילוק 2 מספרים : הטיפוסים הבסיסיים using System; איסוף וניהוף אוספי

More information

הנכות 1 תואיגש םע תודדומתהו תואלול,םיכרעמ : לו 2 גרת

הנכות 1 תואיגש םע תודדומתהו תואלול,םיכרעמ : לו 2 גרת תוכנה 1 תרגול 2: מערכים, לולאות והתמודדות עם שגיאות מערכים Array: A fixed-length data structure for storing multiple values of the same type Example from last week: An array of odd numbers: Indices (start

More information

תרגול מספר 3: מערכים

תרגול מספר 3: מערכים היום בתרגול:.1.2 תרגול מספר 3: מערכים מערך חד-מימדי: מה זה מערך ולמה צריך אותו? איך מגדירים? איך זה נראה בזכרון? דוגמאות לשימוש במערך חד-מימדי השוואה בין משתנה פרימיטיבי למשתנה שאינו פרימיטיבי מערך דו-מימדי:

More information

תוכנה 1 * לא בהכרח בסדר הזה

תוכנה 1 * לא בהכרח בסדר הזה תוכנה 1 תרגול 7: מנשקים, פולימורפיזם ועוד * לא בהכרח בסדר הזה 2 מנשקים מנשקים - תזכורת מנשק )interface( הוא מבנה תחבירי ב- Java המאפשר לחסוך בקוד לקוח. מנשק מכיל כותרות של מתודות )חתימות(. מימוש דיפולטיבי

More information

Type Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works

Type Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works Type Aliases A name may be defined as a synonym for an existing type name. Traditionally, typedef is used for this purpose. In the new standard, an alias declaration can also be used C++11.Thetwoformsareequivalent.

More information

תוכנה 1 בשפת Java נושאים שונים בהורשה רובי בוים ומתי שמרת בית הספר למדעי המחשב אוניברסיטת תל אביב

תוכנה 1 בשפת Java נושאים שונים בהורשה רובי בוים ומתי שמרת בית הספר למדעי המחשב אוניברסיטת תל אביב תוכנה 1 בשפת Java נושאים שונים בהורשה רובי בוים ומתי שמרת בית הספר למדעי המחשב אוניברסיטת תל אביב Today Static vs. Dynamic binding Equals / hashcode String Immutability (maybe) 2 Static versus run-time

More information

תוכנה 1 מערכים. Array Creation and Initialization. Array Declaration. Loop through Arrays. Array Creation and Initialization

תוכנה 1 מערכים. Array Creation and Initialization. Array Declaration. Loop through Arrays. Array Creation and Initialization מערכים תוכנה 1 Array: A fixed-length data structure for storing multiple values of the same type Example from last week: An array of odd numbers: Indices (start from 0) 0 1 2 3 4 5 6 7 תרגול 2: מערכים

More information

תרגול 6 רקורסיה ותכנות מונחה עצמים

תרגול 6 רקורסיה ותכנות מונחה עצמים מבוא למדעי המחשב 2017 תרגול 6 רקורסיה ותכנות מונחה עצמים מבוא למדעי המחשב 1 ראינו בהרצאה רקורסיה תכנות מונחה עצמים: מחלקה ואובייקט שדות, בנאים ושיטות מימוש מערך דינאמי של ראשוניים בתרגול היום רקורסיה הדפסת

More information

תוכנה 1 תרגול מספר 13

תוכנה 1 תרגול מספר 13 1 תוכנה 1 תרגול מספר 13 ו- HashCode Equals עוד על טיפוסים מוכללים )Advanced Generics( חריגים )Exceptions( בית הספר למדעי המחשב אוניברסיטת תל אביב 1 2 ו- HASHCODE EQUALS 3 תזכורת: המחלקה Object package

More information

תוכנה 1 תרגול מספר 13

תוכנה 1 תרגול מספר 13 1 2 תוכנה 1 תרגול מספר 13 ו- HashCode Equals עוד על טיפוסים מוכללים )Advanced Generics( ו- HASHCODE EQUALS חריגים )Exceptions( בית הספר למדעי המחשב אוניברסיטת תל אביב 1 3 4 package java.lang; תזכורת: המחלקה

More information

Due Date: See Blackboard

Due Date: See Blackboard Source File: ~/2305/lab33.C Input: under control of main function Output: under control of main function Value: 3 The Shell sort, named after its inventor Donald Shell, provides a simple and efficient

More information

Computer Programming A תרגול 9

Computer Programming A תרגול 9 Computer Programming A תרגול 9 1 מטרת התרגול הקצאת זיכרון מבנים רשימות דינאמית ניהול הזיכרון בתוכנית עד כה כל המשתנים שראינו היו לוקאליים. משך הקיום של משתנים מקומיים הוא הזמן אשר הפונקציה בה הם נמצאים

More information

תוכנה 1 תרגול 2: מערכים, מבני בקרה ושגיאות

תוכנה 1 תרגול 2: מערכים, מבני בקרה ושגיאות תוכנה 1 תרגול 2: מערכים, מבני בקרה ושגיאות מערכים Array: A fixed-length data structure for storing multiple values of the same type Example: An array of odd numbers: Indices (start from 0) 0 1 2 3 4 5

More information

תוכנה 1. תרגול מספר 11: Static vs. Dynamic Binding מחלקות מקוננות Nested Classes

תוכנה 1. תרגול מספר 11: Static vs. Dynamic Binding מחלקות מקוננות Nested Classes תוכנה 1 תרגול מספר 11: Static vs. Dynamic Binding מחלקות מקוננות Nested Classes class Outer { static class NestedButNotInner {... class Inner {... מחלקות מקוננות NESTED CLASSES 2 מחלקה מקוננת Class) )Nested

More information

תוכנה 1 מערכים. Array Creation and Initialization. Array Declaration. Array Creation and Initialization. Loop through Arrays

תוכנה 1 מערכים. Array Creation and Initialization. Array Declaration. Array Creation and Initialization. Loop through Arrays מערכים Array: A fixed-length data structure for storing multiple values of the same type תוכנה 1 Example: An array of odd numbers: Indices (start from 0) 0 1 2 3 4 5 6 7 odds: 1 3 5 7 9 11 13 15 odds.length

More information

GridKa School 2013: Effective Analysis C++ Standard Template Library

GridKa School 2013: Effective Analysis C++ Standard Template Library GridKa School 2013: Effective Analysis C++ Standard Template Library Introduction Jörg Meyer, Steinbuch Centre for Computing, Scientific Data Management KIT University of the State of Baden-Wuerttemberg

More information

תוכנה 1 3 תרגול מס' מערכים ומבני בקרה

תוכנה 1 3 תרגול מס' מערכים ומבני בקרה תוכנה 1 3 תרגול מס' מערכים ומבני בקרה מערכים Array: A fixed-length data structure for storing multiple values of the same type Example: An array of odd numbers: Indices (start from 0) 0 1 2 3 4 5 6 7 odds:

More information

הנכות 1 םוכיס לוגרת 13 1

הנכות 1 םוכיס לוגרת 13 1 תוכנה 1 סיכום תרגול 13 1 בחינה באופק! הבחינה תכלול את כל הנושאים שכיסינו במהלך הסמסטר: כל ההרצאות כל תרגולים כל תרגילי בית חומר סגור שאלות אמריקאיות 2 קצת על מנשקים מנשק יכול להרחיב שירותים במנשק הם תמיד

More information

Tutorial 10. Introduction to C++ שימו

Tutorial 10. Introduction to C++ שימו Introduction to ++ שימו תרגול זה אינו התרגול הרישמי של הקורס. הוא מבוסס על חוברת התרגולים אך מכיל שינויים, הסברים נוספים ודוגמאות שונות או נוספות. + + תוכנ ית רא שונה ב הכרו ת עם + + תרגול // First ++

More information

תרגול 4 פונקציות. מבנה של פונקציה: public static <return value type> <function name> (<arg1 type> <arg1>, <arg2 type> <arg2>, ) { <function body> }

תרגול 4 פונקציות. מבנה של פונקציה: public static <return value type> <function name> (<arg1 type> <arg1>, <arg2 type> <arg2>, ) { <function body> } נושאי התרגול: מה הן פונקציות הגדרת פונקציה,קריאה לפונקציה העברת ארגומנטים,החזרת ערך או void העברת משתנים פרימיטיביים ומערכים לפונקציה העמסה של פונקציות תרגול 4 פונקציות מוטיבציה לעיתים,אנו נזקקים לבצע

More information

STL components. STL: C++ Standard Library Standard Template Library (STL) Main Ideas. Components. Encapsulates complex data structures and algorithms

STL components. STL: C++ Standard Library Standard Template Library (STL) Main Ideas. Components. Encapsulates complex data structures and algorithms STL: C++ Standard Library Standard Template Library (STL) Encapsulates complex data structures and algorithms is a library of generic container classes which are both efficient and functional C++ STL developed

More information

Introduction to Programming in C תרגול 8

Introduction to Programming in C תרגול 8 Introduction to Programming in C תרגול 8 1 1 נושאים מצביעים רקע אופרטורים על מצביעים מצביעים כפרמטרים לפונקציה הקצאה דינמית מבנים תאור הזיכרון של המחשב: מצביעים ניתן לחשוב על זיכרון המחשב כעל רצף של תאים,

More information

מבוא לתכנות ב- JAVA מעבדה 2

מבוא לתכנות ב- JAVA מעבדה 2 מבוא לתכנות ב- JAVA מעבדה 2 מה בתרגול טיפוסים פרימיטיביים המרות טיפוסים אופרטורים יחסיים ולוגיים משפט if-else בתרגול הקודם טיפוסים פרימיטביים לייצוג מספרים שלמים וממשיים ואופרטורים לפעולות בין מספרים.1

More information

Graph Database, think different!

Graph Database, think different! Graph Database, think different! Written by Roni Licher Winter 2014-2015 236363 - Database Systems - Technion Nodes Edges (directed or not) Properties Neo4j and Cypher 4j Graph database (Like SQL server

More information

STL: C++ Standard Library

STL: C++ Standard Library STL: C++ Standard Library Encapsulates complex data structures and algorithms CSC 330 OO Software Design 1 We ve emphasized the importance of software reuse. Recognizing that many data structures and algorithms

More information

תרגול 7 רשימות משורשרות, רקורסיית

תרגול 7 רשימות משורשרות, רקורסיית מבוא למדעי המחשב 2018 תרגול 7 רשימות משורשרות, רקורסיית זנב 1 ראינו בהרצאה רשימות משורשרות רקורסיית זנב 2 בתרגול היום רשימות משורשרות עוד שיטות מחלקה רקורסיית זנב היפוך מחרוזות, חיפוש בינארי 3 רשימות משורשרות

More information

Unit 1: Preliminaries Part 4: Introduction to the Standard Template Library

Unit 1: Preliminaries Part 4: Introduction to the Standard Template Library Unit 1: Preliminaries Part 4: Introduction to the Standard Template Library Engineering 4892: Data Structures Faculty of Engineering & Applied Science Memorial University of Newfoundland May 6, 2010 ENGI

More information

תוכנה 1 מבני נתונים גנריים

תוכנה 1 מבני נתונים גנריים תוכנה 1 מבני נתונים גנריים תרגול 8 2 Java Collections Framework Collection: a group of elements Interface Based Design: Java Collections Framework Interfaces Implementations Algorithms 3 Online Resources

More information

פתרון מוצע לבחינת מה"ט ב_שפת c מועד אביב תשע"ח, פברואר 8102 מחבר: מר שייקה בילו, מכללת אורט רחובות

פתרון מוצע לבחינת מהט ב_שפת c מועד אביב תשעח, פברואר 8102 מחבר: מר שייקה בילו, מכללת אורט רחובות פתרון מוצע לבחינת מה"ט ב_שפת c מועד אביב תשע"ח, פברואר 8102 מחבר: מר שייקה בילו, מכללת אורט רחובות שאלה מספר 1 התוכנית מגדירה חמישה משתנים שלמים: השלושה הראשונים הם שלושה מצביעים - *s *t,i. j ושלושה נוספים

More information

Exams questions examples

Exams questions examples Exams questions examples 1 Exam example 1. y - x what נק' ( לפניך הפעולה הרקורסיבית מקבלת כפרמטרים שני מספרים שלמים ו 10 )? מה יהיה הפלט כתוצאה מזימון הפעולה what public static int what(int x, int y) if(x

More information

קורס תכנות שיעור שישי: מחרוזות, מצביעים

קורס תכנות שיעור שישי: מחרוזות, מצביעים קורס תכנות שיעור שישי: מחרוזות, מצביעים מערכים אוסף סדור של משתנים מאותו סוג המשתנים נמצאים ברצף בזיכרון העברת מערך לפונקציה Define רקורסיה במערך מערכים דו מימדיים 2 מחרוזות מהי מחרוזת? רצף של תוים ייצוג

More information

CS11 Advanced C++ Fall Lecture 1

CS11 Advanced C++ Fall Lecture 1 CS11 Advanced C++ Fall 2006-2007 Lecture 1 Welcome! ~8 lectures Slides are posted on CS11 website http://www.cs.caltech.edu/courses/cs11 ~6 lab assignments More involved labs 2-3 week project at end CS

More information

מבוא לתכנות ב- JAVA מעבדה 4

מבוא לתכנות ב- JAVA מעבדה 4 מבוא לתכנות ב- JAVA מעבדה 4 מה בתרגול מערכים מחרוזות מערך חד מימדי מערך הוא מבנה המחזיק סדרה של איברים מאותו טיפוס גודל המערך הוא קבוע )נקבע בעת יצירת המערך( הגישה לכל איבר היא באמצעות אינדקס למה לי מערך?

More information

קורס תכנות כתובות בזיכרון כתובות בזכרון מצביעים וכתובות מצביעים וכתובות שיעור שביעי: מבנים, הקצאת זיכרון דינאמית האופרטור &

קורס תכנות כתובות בזיכרון כתובות בזכרון מצביעים וכתובות מצביעים וכתובות שיעור שביעי: מבנים, הקצאת זיכרון דינאמית האופרטור & כתובות בזיכרון קורס תכנות int x = 10; char c = a ; 10 784658 'a' 26537 שם x כתובת 784658 שיעור שביעי: מבנים, הקצאת זיכרון דינאמית 26537 c 1 הגדרת מצביע variable( )pointer כתובות בזכרון האופרטור & מחזיר

More information

עצים. מבני נתונים Iterators רשימות מקושרות עצים "רגילות" רקורסיביות

עצים. מבני נתונים Iterators רשימות מקושרות עצים רגילות רקורסיביות עצים 1 מבני נתונים Iterators רשימות מקושרות "רגילות" רקורסיביות עצים 2 1 עצים בינאריים סריקות על עצים עצי חיפוש מימוש Iterators לסריקה 3 עץ בינארי הינו מבנה נתונים המייצג עץ מושרש )כלומר עם שורש( עץ בינארי

More information

Programming in C++ using STL. Rex Jaeschke

Programming in C++ using STL. Rex Jaeschke Programming in C++ using STL Rex Jaeschke Programming in C++ using STL 1997, 1999, 2002, 2007, 2009 Rex Jaeschke. All rights reserved. Edition: 2.0 All rights reserved. No part of this publication may

More information

קורס תכנות בשיעור הקודם למדנו על רקורסיה שיעור שישי: מערכים פונקציה רקורסיבית שאלה חישוב נוסחאות רקורסיביות בשפת C

קורס תכנות בשיעור הקודם למדנו על רקורסיה שיעור שישי: מערכים פונקציה רקורסיבית שאלה חישוב נוסחאות רקורסיביות בשפת C בשיעור הקודם למדנו על רקורסיה פתרנו את בעיית מגדלי הנוי בעזרת רקורסיה כלומר בעזרת פונקציה שקוראת לעצמה. רקורסיה מאפשרת לנו לפתור בעיה "גדולה" בעזרת פתרון של בעיות "קטנות" המרכיבות אותה. קורס תכנות שיעור

More information

רשימות דילוגים Skip Lists

רשימות דילוגים Skip Lists Lecture6 of Geiger & Itai s slide brochure www.cs.technion.ac.il/~dang/courseds רשימות דילוגים Skip Lists Skip lists: A probabilistic Alternative to Balanced Trees, William Pugh, Communications of the

More information

תרגול 3 מערכים ופונקציות

תרגול 3 מערכים ופונקציות מבוא למדעי המחשב 2017 תרגול 3 מערכים ופונקציות מערכים מאפשרים עבודה עם מקבצים של נתונים פונקציות מאפשרות אבסטרקציה והאחדה של הקוד ראינו בהרצאה מערכים הצהרה, אתחול, גישה לאיברים במערך יצוג בזיכרון ובטבלת

More information

Today. andyoucanalsoconsultchapters6amd7inthetextbook. cis15-fall2007-parsons-lectvii.1 2

Today. andyoucanalsoconsultchapters6amd7inthetextbook. cis15-fall2007-parsons-lectvii.1 2 TEMPLATES Today This lecture looks at techniques for generic programming: Generic pointers Templates The standard template library Thebestreferenceis: http://www.cppreference.com/index.html andyoucanalsoconsultchapters6amd7inthetextbook.

More information

הנכות 1 םוכיס לוגרת 14 1

הנכות 1 םוכיס לוגרת 14 1 תוכנה 1 סיכום תרגול 14 1 קצת על מנשקים מנשק יכול להרחיב יותר ממנשק אחד שירותים במנשק הם תמיד מופשטים וציבוריים public interface MyInterface { public abstract int foo1(int i); int foo2(int i); The modifiers

More information

Due Date: See Blackboard

Due Date: See Blackboard Source File: ~/2315/33/lab33.cpp Input: under control of main function Output: under control of main function Value: 3 The Shell sort, named after its inventor Donald Shell, provides a simple and efficient

More information

Templates and Vectors

Templates and Vectors Templates and Vectors 1 Generic Programming function templates class templates 2 the STL vector class a vector of strings enumerating elements with an iterator inserting and erasing 3 Writing our own vector

More information

Practical Session #4 - ADTs: Array, Queue, Stack, Linked List

Practical Session #4 - ADTs: Array, Queue, Stack, Linked List Practical Session #4 - ADTs: Array, Queue, Stack, Linked List Basic Data Structures and Abstract Data Types ADT Array Abstract Data Type A collection of data-storing entities with operations to create,

More information

Nir Adar

Nir Adar שפת Java למתכנתי ++C - חלק שני מסמך זה הורד מהאתר. אין להפיץ מסמך זה במדיה כלשהי, ללא אישור מפורש מאת המחבר. מחבר המסמך איננו אחראי לכל נזק, ישיר או עקיף, שיגרם עקב השימוש במידע המופיע במסמך, וכן לנכונות

More information

Template based set of collection classes STL collection types (container types)

Template based set of collection classes STL collection types (container types) STL Collection Types Template based set of collection classes STL collection types (container types) Sequences vector - collection of elements of type T list - doubly linked list, only sequential access

More information

- MEAN Stack חזרה. MongoDB - as the database Express - as the web framework AngularJS - as the frontend framework NodeJS- as the server platform

- MEAN Stack חזרה. MongoDB - as the database Express - as the web framework AngularJS - as the frontend framework NodeJS- as the server platform הדר פיקאלי תשע"ו - 2016 - MEAN Stack חזרה בניית web applications כרוכה בשימוש בטכנולוגיות וכלים שונים, להתמודדות עם: מסד נתונים, פעולות בצד השרת, טיפול בצד הלקוח והצגה של הנתונים. מהו?MEAN "MEAN is a fullstack

More information

Communication Networks ( ) / Spring 2011 The Blavatnik School of Computer Science, Tel-Aviv University. Allon Wagner

Communication Networks ( ) / Spring 2011 The Blavatnik School of Computer Science, Tel-Aviv University. Allon Wagner Communication Networks (0368-3030) / Spring 2011 The Blavatnik School of Computer Science, Tel-Aviv University Allon Wagner Kurose & Ross, Chapter 3.5.5, 3.7 (5 th ed.) Many slides adapted from: J. Kurose

More information

דף הדרכה ליצירת שרת/ לקוח עם GUI

דף הדרכה ליצירת שרת/ לקוח עם GUI דף הדרכה ליצירת שרת/ לקוח עם GUI בשיעורים הקודמים למדנו כיצד ליצור שרת לקוח פשוט, ויצירת טופס המכיל פקדים כלומר יצירת GUI למשתמש, בשיעור זה נרצה להראות את הדרך לשילוב בין השניים כלומר ליצור לקוח client

More information

Chapter 5. The Standard Template Library.

Chapter 5. The Standard Template Library. Object-oriented programming B, Lecture 11e. 1 Chapter 5. The Standard Template Library. 5.1. Overview of main STL components. The Standard Template Library (STL) has been developed by Alexander Stepanov,

More information

The Standard Template Library Classes

The Standard Template Library Classes The Standard Template Library Classes Lecture 33 Sections 9.7, 9.8 Robb T. Koether Hampden-Sydney College Wed, Apr 23, 2014 Robb T. Koether (Hampden-Sydney College) The Standard Template Library Classes

More information

פרק 15 טיפוס חדש: מבנים שימוש במבנים שימוש במבנים שימוש במבנים

פרק 15 טיפוס חדש: מבנים שימוש במבנים שימוש במבנים שימוש במבנים פרק 15 ב- C מבנים טיפוס חדש: מבנים 1 מבנה (structure) הוא טיפוס מורכב בשפת C (בניגוד לטיפוס בסיסי). זהו טיפוס שמיועד לאיחוד קבוצת משתנים תחת שם אחד. משתנים אלו נקראים השדות של המבנה, והם יכולים להיות מטיפוסים

More information

Chapter 11.2 Linked lists ( )

Chapter 11.2 Linked lists ( ) Lecture of Geiger & Itai s slide brochure www.cs.technion.ac.il/~dang/courseds מערכים, מטריצות דלילות, ורשימות מקושרות חומר קריאה לשיעור זה Chapter. Linked lists ( ) Geiger & Itai, מערך מוגדר ע"י הפעולות

More information

תוכנה 1 טיפוסי השפה טיפוסים לא פרימיטיביים הטיפוסים הפרימיטיביים מחרוזות המרה למספרים תרגול 2: טיפוסי שפה, מחרוזות, מערכים ושגיאות

תוכנה 1 טיפוסי השפה טיפוסים לא פרימיטיביים הטיפוסים הפרימיטיביים מחרוזות המרה למספרים תרגול 2: טיפוסי שפה, מחרוזות, מערכים ושגיאות טיפוסי השפה תוכנה 1 תרגול 2: טיפוסי שפה, מחרוזות, מערכים ושגיאות טיפוסים יסודיים (פרימיטיביים): 8 טיפוסים מוגדרים בשפה שמיועדים להכיל ערכים פשוטים: מספרים שלמים: byte, short, int, long מספרים ממשיים: float,

More information

Container Notes. Di erent Kinds of Containers. Types Defined by Containers. C++11 Container Notes C++11

Container Notes. Di erent Kinds of Containers. Types Defined by Containers. C++11 Container Notes C++11 Di erent Kinds of Containers Container Notes A container is an object that stores other objects and has methods for accessing the elements. There are two fundamentally di erent kinds of containers: Sequences

More information

THE STANDARD TEMPLATE LIBRARY (STL) Week 6 BITE 1513 Computer Game Programming

THE STANDARD TEMPLATE LIBRARY (STL) Week 6 BITE 1513 Computer Game Programming THE STANDARD TEMPLATE LIBRARY (STL) Week 6 BITE 1513 Computer Game Programming What the heck is STL???? Another hard to understand and lazy to implement stuff? Standard Template Library The standard template

More information

מבני נתונים תכנות מונחה עצמים מבני נתונים. מחלקות אבסטרקטיות חבילות packages סיכום הרשאות גישה wrappers ADT מערך דינמי מחסנית

מבני נתונים תכנות מונחה עצמים מבני נתונים. מחלקות אבסטרקטיות חבילות packages סיכום הרשאות גישה wrappers ADT מערך דינמי מחסנית מבני נתונים 1 תכנות מונחה עצמים מחלקות אבסטרקטיות חבילות packages סיכום הרשאות גישה wrappers מבני נתונים ADT מערך דינמי מחסנית 2 1 מבני נתונים תור - Queue Iterators רשימות מקושרות "רגילות" 3 מבנה נתונים

More information

STL Quick Reference for CS 241

STL Quick Reference for CS 241 STL Quick Reference for CS 241 Spring 2018 The purpose of this document is to provide basic information on those elements of the C++14 standard library we think are most likely to be needed in CS 241.

More information

Module 9. Templates & STL

Module 9. Templates & STL Module 9 Templates & STL Objectives In this module Learn about templates Construct function templates and class templates STL 2 Introduction Templates: enable you to write generic code for related functions

More information

Computational Physics

Computational Physics Computational Physics numerical methods with C++ (and UNIX) 2018-19 Fernando Barao Instituto Superior Tecnico, Dep. Fisica email: fernando.barao@tecnico.ulisboa.pt Computational Physics 2018-19 (Phys Dep

More information

Arrays - Vectors. Arrays: ordered sequence of values of the same type. Structures: named components of various types

Arrays - Vectors. Arrays: ordered sequence of values of the same type. Structures: named components of various types Arrays - Vectors Data Types Data Type: I. set of values II. set of operations over those values Example: Integer I. whole numbers, -32768 to 32767 II. +, -, *, /, %, ==,!=, , =,... Which operation

More information

הנכות 1 םוכיס לוגרת 13 1

הנכות 1 םוכיס לוגרת 13 1 תוכנה 1 סיכום תרגול 13 1 בחינה באופק! הבחינה תכלול את כל הנושאים שכיסינו במהלך הסמסטר: כל ההרצאות כל תרגולים כל תרגילי בית חומר סגור שאלות אמריקאיות 2 קצת על מנשקים מנשק יכול להרחיב יותר ממנשק אחד שירותים

More information

G52CPP C++ Programming Lecture 18

G52CPP C++ Programming Lecture 18 G52CPP C++ Programming Lecture 18 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Welcome Back 2 Last lecture Operator Overloading Strings and streams 3 Operator overloading - what to know

More information

1. The term STL stands for?

1. The term STL stands for? 1. The term STL stands for? a) Simple Template Library b) Static Template Library c) Single Type Based Library d) Standard Template Library Answer : d 2. Which of the following statements regarding the

More information

by Pearson Education, Inc. All Rights Reserved. 2

by Pearson Education, Inc. All Rights Reserved. 2 An important part of every container is the type of iterator it supports. This determines which algorithms can be applied to the container. A vector supports random-access iterators i.e., all iterator

More information

כתבו קוד ב- 3 קבצי ה hpp (כתבו כהערה את שם הקובץ מעל) כך שהקוד יהיה תקין ובסגנון טוב. אין חובה

כתבו קוד ב- 3 קבצי ה hpp (כתבו כהערה את שם הקובץ מעל) כך שהקוד יהיה תקין ובסגנון טוב. אין חובה פקולטה: מדעי הטבע מחלקה: מדעי המחשב שם הקורס: מבנה זכרון ושפת ++C קוד הקורס: 7027810 תאריך בחינה: שאלות לדוגמא משך הבחינה: שעתיים שם המרצים: ד"ר אופיר פלא, ד"ר מירי בן ניסן חומר עזר: פתוח שימוש במחשבון:

More information

מבוא למדעי המחשב 2018 תרגול 7

מבוא למדעי המחשב 2018 תרגול 7 מבוא למדעי המחשב 2018 תרגול 7 רשימות משורשרות, רקורסיית זנב 1 ראינו בהרצאה רשימות משורשרות רקורסיית זנב 2 בתרגול היום רשימות משורשרות עוד שיטות מחלקה רקורסיית זנב היפוך מחרוזות, חיפוש בינארי 3 רשימות משורשרות

More information

קורס תכנות שיעור שני: שימוש במשתנים,

קורס תכנות שיעור שני: שימוש במשתנים, קורס תכנות שיעור שני: שימוש במשתנים, בקרת זרימה, לולאות 1 נושאי השיעור היום משתנים )variables( טיפוסי משתנים בשפת C הגדרת משתנים השמה למשתנים פעולות על משתנים קליטת ערכים מהמשתמש הדפסה משתנים בקרת זרימה

More information

CS197c: Programming in C++

CS197c: Programming in C++ CS197c: Programming in C++ Lecture 2 Marc Cartright http://ciir.cs.umass.edu/~irmarc/cs197c/index.html Administration HW1 will be up this afternoon Written assignment Due in class next week See website

More information